#### prepare packages and functions ####
require(estimatr)
require(dichromat)

issue.var.name <- c("a.article9", "b.defense", "c.revisionism", "d.women", 
                    "e.gay", "f.immigrant", "g.growth", "h.tax")

color <- colorschemes$Categorical.12
plot.col <- c(color[12], color[12], color[12], 
              color[10], color[10], color[10], 
              color[2], color[2])

issue.label <- c("Revision of Article 9", "Increasing Defense Power", 
                 "Historical Revisionism", "Women's Empowerment", 
                 "Gay Marriage", "Accepting Foreign Workers", 
                 "Economic Growth", "Progressive Tax")

# function to compute MMs
MM <- function(data, attribute, outcome, id) {
  lm.data <- data.frame(id = data[, id], A = data[, attribute], 
                        Y = data[, outcome])
  l <- nlevels(lm.data$A)
  result.matrix <- matrix(NA, l, 3)
  for (i in 1:l) {
    level.label <- levels(lm.data$A)[i]
    result.matrix[i, ] <- unlist(lm_robust(Y ~ 1, data = lm.data, 
                                           subset = A == level.label, 
                                           clusters = id)[c(1, 6, 7)])
  }
  result.matrix
}

#### load data ####
# task-level dataset
task.data <- read.csv("task_data.csv")

# reorder the levels of attribute variables
position.label <- c("Agree", "Neither", "Disagree")
task.data$a.article9 <- factor(task.data$a.article9, levels = position.label)
task.data$b.defense <- factor(task.data$b.defense, levels = position.label)
task.data$c.revisionism <- factor(task.data$c.revisionism, levels = position.label)
task.data$d.women <- factor(task.data$d.women, levels = position.label)
task.data$e.gay <- factor(task.data$e.gay, levels = position.label)
task.data$f.immigrant <- factor(task.data$f.immigrant, levels = position.label)
task.data$g.growth <- factor(task.data$g.growth, levels = position.label)
task.data$h.tax <- factor(task.data$h.tax, levels = position.label)

# respondent-level dataset
respondent.data <- read.csv("respondent_data.csv")

#### analysis ####
## number of observations
sum(! is.na(respondent.data$ideology))

## simple regressions
simple.lm <- matrix(NA, 8, 3)
for (i in 1:8) {
  lm.result <- lm_robust(respondent.data$ideology ~ respondent.data[, i + 9])
  simple.lm[i, 1] <- lm.result$coefficients[2]
  simple.lm[i, 2] <- lm.result$std.error[2]
  simple.lm[i, 3] <- lm.result$p.value[2]
}

# Table A.13
round(simple.lm, 3)

## multiple regressions
# without covariates
multiple.lm.result.1 <- lm_robust(ideology ~ article9.attitude + defense.attitude + 
                                    revisionism.attitude + women.attitude + 
                                    gay.attitude + immigrant.attitude + 
                                    growth.attitude + tax.attitude, 
                                  respondent.data)

# with covariates
multiple.lm.result.2 <- lm_robust(ideology ~ article9.attitude + defense.attitude + 
                                    revisionism.attitude + women.attitude + 
                                    gay.attitude + immigrant.attitude + 
                                    growth.attitude + tax.attitude + 
                                    gender + age + I(age ^ 2) + 
                                    middle.edu + high.edu + 
                                    DID.ratio + factor(knowledge), 
                                  respondent.data)

# Table A.14
round(cbind(multiple.lm.result.1$coefficients, 
            multiple.lm.result.1$std.error, 
            multiple.lm.result.1$p.value), 3)
round(cbind(multiple.lm.result.2$coefficients, 
            multiple.lm.result.2$std.error, 
            multiple.lm.result.2$p.value), 3)

## compute MMs only for respondents who reported their ideological self-placement
choice.result.ideology <- rating.result.ideology <- matrix(NA, 24, 3)
for (i in 1:8) {
  choice.result.ideology[(3 * (i - 1) + 1):(3 * i), ] <- 
    MM(subset(task.data, ! is.na(ideology)), issue.var.name[i], 
       "choice", "respondent.id")
  rating.result.ideology[(3 * (i - 1) + 1):(3 * i), ] <- 
    MM(subset(task.data, ! is.na(ideology)), issue.var.name[i], 
       "rating", "respondent.id")
}

# original results
choice.result <- rating.result <- matrix(NA, 24, 3)
for (i in 1:8) {
  choice.result[(3 * (i - 1) + 1):(3 * i), ] <- MM(task.data, 
                                                   issue.var.name[i], 
                                                   "choice", 
                                                   "respondent.id")
  rating.result[(3 * (i - 1) + 1):(3 * i), ] <- MM(task.data, 
                                                   issue.var.name[i], 
                                                   "rating", 
                                                   "respondent.id")
}

# Figure A.11
cairo_pdf("Figure_A11.pdf", width = 6.2, height = 4, pointsize = 8)
par(mar = c(3, 0, 1.5, 0), lwd = 0.5, xpd = TRUE)
plot(NULL, NULL, type = "n", bty = "n", xlim = c(0.23, 0.85), ylim = c(0, 33), 
     xlab = "", ylab = "", xaxt = "n", yaxt = "n")
segments(seq(0.4, 0.6, 0.05), 0, seq(0.4, 0.6, 0.05), 33, lwd = 0.4, col = "gray")
for (i in 1:8) {
  segments(0.395, 35 - 4 * i, 0.605, 35 - 4 * i, lty = 3, col = "gray")
  segments(0.395, 34 - 4 * i, 0.605, 34 - 4 * i, lty = 3, col = "gray")
  segments(0.395, 33 - 4 * i, 0.605, 33 - 4 * i, lty = 3, col = "gray")
  segments(choice.result.ideology[(3 * (i - 1) + 1):(3 * i), 2], 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, 
           choice.result.ideology[(3 * (i - 1) + 1):(3 * i), 3], 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, col = plot.col[i])
  points(choice.result.ideology[(3 * (i - 1) + 1):(3 * i), 1], 
         c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, 
         pch = 19, col = plot.col[i])
  segments(choice.result[(3 * (i - 1) + 1):(3 * i), 2], 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, 
           choice.result[(3 * (i - 1) + 1):(3 * i), 3], 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, col = plot.col[i])
  points(choice.result[(3 * (i - 1) + 1):(3 * i), 1], 
         c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, 
         pch = 21, col = plot.col[i], bg = "white")
}
axis(1, at = seq(0.4, 0.6, 0.05))
segments(seq(0.65, 0.85, 0.05), 0, seq(0.65, 0.85, 0.05), 33, lwd = 0.4, col = "gray")
for (i in 1:8) {
  segments(0.645, 35 - 4 * i, 0.855, 35 - 4 * i, lty = 3, col = "gray")
  segments(0.645, 34 - 4 * i, 0.855, 34 - 4 * i, lty = 3, col = "gray")
  segments(0.645, 33 - 4 * i, 0.855, 33 - 4 * i, lty = 3, col = "gray")
  segments(0.75 + (rating.result.ideology[(3 * (i - 1) + 1):(3 * i), 2] - 5.5) / 4, 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, 
           0.75 + (rating.result.ideology[(3 * (i - 1) + 1):(3 * i), 3] - 5.5) / 4, 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, col = plot.col[i])
  points(0.75 + (rating.result.ideology[(3 * (i - 1) + 1):(3 * i), 1] - 5.5) / 4, 
         c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) + 0.2, 
         pch = 19, col = plot.col[i])
  segments(0.75 + (rating.result[(3 * (i - 1) + 1):(3 * i), 2] - 5.5) / 4, 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, 
           0.75 + (rating.result[(3 * (i - 1) + 1):(3 * i), 3] - 5.5) / 4, 
           c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, col = plot.col[i])
  points(0.75 + (rating.result[(3 * (i - 1) + 1):(3 * i), 1] - 5.5) / 4, 
         c(35 - 4 * i, 34 - 4 * i, 33 - 4 * i) - 0.2, 
         pch = 21, col = plot.col[i], bg = "white")
}
axis(1, at = seq(0.65, 0.85, 0.05), labels = seq(5.1, 5.9, 0.2))
for (i in 1:8) {
  text(0.23, 36 - 4 * i, labels = issue.label[i], pos = 4)
  text(0.33, 35 - 4 * i, labels = "Agree", cex = 0.9, pos = 4)
  text(0.33, 34 - 4 * i, labels = "Neither", cex = 0.9, pos = 4)
  text(0.33, 33 - 4 * i, labels = "Disagree", cex = 0.9, pos = 4)
}
legend(0.23, 0, c("Ideolgical ID reported", "Original sample"), 
       pch = c(19, 21), bg = c(NA, "white"), bty = "n")
mtext(c("Choice", "Rating"), at = c(0.5, 0.75), cex = 1.2, font = 2)
dev.off()